<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relative}}</span>
            <van-button size="mini" type="default" @click="clickCommentReply(item)">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" @click="toggleCommentLike(item)" :name="item.is_liking?'like':'like-o'" />
      </van-cell>

      <van-popup v-if="isShowReply" v-model="isShowReply" round position="bottom" :style="{ height: '85%' }">
        <comment-reply @off-reply='isShowReply=false' :comment='currentComment' :article-id='articleId'></comment-reply>
      </van-popup>

    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        v-model.trim="content"
        placeholder="请输入评论内容"
      >
        <van-button @click="addComment" slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import { reqAddComment, reqGetComment, reqAddCommentLike, reqDeleteCommentLike } from '@/api/comment.js'
import CommentReply from './commetReply.vue'
export default {
  name: 'ArticleComment',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      content: '',
      offset: null,
      isShowReply: false,
      currentComment: ''
    }
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  components: {
    CommentReply
  },

  methods: {
    async addComment () {
      if (this.content.trim() === '') return
      const res = await reqAddComment(this.articleId, this.content)
      //   console.log(res.data.data.new_obj)
      this.list.unshift(res.data.data.new_obj)
      this.content = ''
      this.$toast.success('评论成功')
    },
    async onLoad () {
      const res = await reqGetComment(this.articleId, this.offset)
      console.log(res.data)
      const arr = res.data.data.results
      this.list = [...this.list, ...arr]
      this.loading = false
      // 判断数据是否加载完成
      if (res.data.data.last_id === res.data.data.end_id) {
        this.finished = true
      }
      // 更新offset
      this.offset = res.data.data.last_id
    },
    async toggleCommentLike (comment) {
      try {
        const commentId = comment.com_id.toString()
        if (comment.is_liking) {
          await reqDeleteCommentLike(commentId)
          this.$toast.success('取消评论点赞成功')
          // comment.is_liking = false
        } else {
          await reqAddCommentLike(commentId)
          this.$toast.success('评论点赞成功')
          // comment.is_liking = true
        }
        comment.is_liking = !comment.is_liking
      } catch (err) {
        console.log(err)
      }
    },
    clickCommentReply (item) {
      this.isShowReply = true
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
.van-icon::before{
  color: red;

}
</style>
